<template>
  <view class="download-container">
    <button @click="downloadImg">下载网络图片</button>
    <image v-if="imagePath" :src="imagePath" mode="widthFix"></image>
  </view>
</template>

<script>
export default {
  data() {
    return {
      imagePath: '',
      imageURL: "https://cdn.pixabay.com/photo/2025/11/05/20/57/monastery-9939590_1280.jpg"
    }
  },
  methods: {
    downloadImg() {
      uni.downloadFile({
        url: this.imageURL,
        success: (res) => {
          if (res.statusCode === 200) {
            this.imagePath = res.tempFilePath
            console.log('下载完成，本地路径：', this.imagePath)
          }
        },
        progress: (res) => {
          console.log('下载进度：', res.progress, '%')
        },
        fail: (err) => {
          console.error('下载失败', err)
        }
      })
    }
  }
}
</script>

<style scoped>
.download-container {
  padding: 20rpx;
}
image {
  width: 100%;
  margin-top: 20rpx;
}
</style>